package com.phpxiu.app.view;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;

/**
 * Created by WuJinZhou on 2016/5/6.
 */
public class UIBanner extends UIBase implements View.OnClickListener{
    public final static String TAG="BannerWeb";
    public final static String WEB_CACHE_DIRNAME="web.cache";
    public final static String BANNER_WEB_TITLE="EXTRA_BANNER_WEB_TITLE";
    public final static String BANNER_WEB_URL="EXTRA_BANNER_WEB_URL";
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_hot_banner_web_view);
        webView=(WebView)findViewById(R.id.home_hot_banner_web_view);
        webView.setWebViewClient(new MWebViewClient());
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        //webView.getSettings().setJavaScriptEnabled(true);// 设置webView可以与js交互
        String url=getIntent().getStringExtra(BANNER_WEB_URL);
        if(url!=null&&!url.equals("")) {
            webView.loadUrl(url);
            //webView.loadUrl("http://show.1198.com/live/14527");
        }
        TextView title=(TextView)findViewById(R.id.title);
        title.setText(getIntent().getStringExtra(BANNER_WEB_TITLE));
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
    }

    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()== R.id.top_bar_back_btn){
            onBackPressed();
        }
    }
    class MWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
